package  com.manager.car.action;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.omg.CORBA.Request;

import com.google.gson.Gson;
import com.manager.car.po.SubscribeInfo;
import com.manager.car.service.ISubscribeService;
import com.manager.car.util.Page;
import com.manager.car.util.PageBean;
import com.manager.car.util.Verification;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;




public class SubscribeAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private SubscribeInfo sub;
	private Integer id;
	private ISubscribeService subService;//接口
	private int  page;
	private int limit;
    private String code;
    private String subName;

    HttpServletResponse response = ServletActionContext.getResponse();
    HttpSession session = ServletActionContext.getRequest().getSession();
   
	@Override//验证码
	public String execute() throws Exception {
		String code = Verification.getRandCode(4);
		System.out.println(code + "@t434235ddd");
		BufferedImage image = Verification.getImage(100, 30, code, 5);
		session.setAttribute("Yz", code);
		ImageIO.write(image, "jpg", response.getOutputStream());
		//ServletActionContext.getResponse().getWriter().print(code);
		return NONE;

	}
	//向前台输入验证码
	public void yz() throws IOException {
		String code= (String) session.getAttribute("Yz");
		System.out.println("验证码"+code);
		response.getWriter().println(code);
	}

	/**
    * 查询所有
    *
    * @return
     * @throws IOException 
    */
    public void findAll() throws IOException  {
	    response.setContentType("application/json");
	    response.setCharacterEncoding("utf-8");
	    PageBean< SubscribeInfo>pb=subService.findAll(sub, page, limit);
	
	    Gson json=new Gson();
	    String str = json.toJson(pb);
	    response.getWriter().println(str);
   
   }
    //查询
    public void queryAll() throws IOException  {
	    response.setContentType("application/json");
	    response.setCharacterEncoding("utf-8");
	   //查询数据
	    subName=subName==null?"":subName;
	    subName=subName+"s";
	    List<SubscribeInfo> list= subService.queryAll(subName, limit, page);
	    int count=subService.getcount(subName, limit, page);
	     Page<SubscribeInfo> page=new Page<SubscribeInfo>();
	     
	     page.setDate(list);
	     page.setCount(count);
	     page.setCode(0);
	     page.setMsg("失败");
	     
	    Gson json=new Gson();
	    String str = json.toJson(page);
	    System.out.println(str);
	    response.getWriter().println(str);
  
  }
  //查询
    public void queryAllw() throws IOException  {
	    response.setContentType("application/json");
	    response.setCharacterEncoding("utf-8");
	   //查询数据
	    subName=subName==null?"":subName;
	    subName=subName+"w";
	    List<SubscribeInfo> list= subService.queryAll(subName, limit, page);
	    int count=subService.getcount(subName, limit, page);
	     Page< SubscribeInfo> page=new Page<SubscribeInfo>();
	     
	     page.setDate(list);
	     page.setCount(count);
	     page.setCode(0);
	     page.setMsg("失败");
	     
	    Gson json=new Gson();
	    String str = json.toJson(page);
	    System.out.println(str);
	    response.getWriter().println(str);
  
  }
    //根据ID查询
    public String queryById() {
    	 sub= subService.queryById(id);
    	String time= sub.getSubTime().replace(" ","T");
    	sub.setSubTime(time);
    	System.out.println(sub.getSubName()+"名字");
    	ActionContext.getContext().put("subInfo", sub);
    	//ServletActionContext.getRequest().setAttribute("subInfo", sub);
    	return "update";
    }
    //修改
    public void update() throws IOException {
    	subService.updateSub(sub);
    	response.getWriter().print(true);
    	
    }
    //修改维修状态
    public void updateSt() throws IOException {
    	sub= subService.queryById(id);
    	sub.setSubStatic(1);
    	subService.updateSub(sub);
    	response.getWriter().print(1);
    	}
    //添加
    public void add() throws IOException {
    	subService.addSub(sub);
		response.getWriter().println(true);
    }
	public SubscribeInfo getSub() {
		return sub;
	}
	public void setSub(SubscribeInfo sub) {
		this.sub = sub;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public ISubscribeService getSubService() {
		return subService;
	}
	public void setSubService(ISubscribeService subService) {
		this.subService = subService;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getSubName() {
		return subName;
	}
	public void setSubName(String subName) {
		this.subName = subName;
	}
	public HttpServletResponse getResponse() {
		return response;
	}
	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}
	public HttpSession getSession() {
		return session;
	}
	public void setSession(HttpSession session) {
		this.session = session;
	}

}
